//
// Created by wjc13 on 2017/8/5.
//
#include "sqlist.h"

Status InitList(struct SqList *L) {
    L->length = 0;
    return True;
}

int ListLength(struct SqList L) {
    return L.length;
}

Status EmptyList(struct SqList *L) {
    L->length = 0;
    return True;
}

Status InsertElem(struct SqList *L, int location, ElemType elem) {
    if (L->length >= MAX_SIZE) {
        printf("[Warning] insert elem overflow\n");
        return OVERFLOW;
    }
    if (location > L->length) {
        location = L->length;
    }
    if (location < 0) {
        location = 0;
    }
    for (int i = L->length; i > location; --i) {
        L->data[i] = L->data[i-1];
    }
    L->data[location] = elem;
    L->length++;
    return True;
}

Status DeleteElem(struct SqList *L, int location, ElemType *type) {
    if (L->length <= 0) {
        return False;
    }
    if (location > L->length) {
        location = L->length;
    }
    if (location < 0) {
        location = 0;
    }
    *type = L->data[location];
    for (int i = location; i < L->length - 1; ++i) {
        L->data[i] = L->data[i + 1];
    }
    L->length--;
    return True;

}

Status GetElem(struct SqList L, int location, ElemType *type) {
    if (location >= L.length || location < 0) {
        return False;
    }
    *type = L.data[location];
    return True;
}

int LocateElem(struct SqList L, ElemType elem) {
    for (int i = 0; i < L.length; ++i) {
        if (L.data[i] == elem) {
            return i;
        }
    }
    return -1;
}

Status IsListEmpty(struct SqList L) {
    if (L.length == 0) {
        return True;
    }
    return False;
}

